package com.sangmo.fts.user.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.boot.framework.common.session.UserSession;
import com.sangmo.fts.clazz.interfaces.UserCreation;
import com.sangmo.fts.clazz.model.Clazz;
import com.sangmo.fts.clazz.service.ClazzEntityService;
import com.sangmo.fts.clazz.service.ClazzService;
import com.sangmo.fts.clazz.service.UserClazzEntityService;
import com.sangmo.fts.external.ExternalManagerService;
import com.sangmo.fts.external.response.LoginInfo;
import com.sangmo.fts.user.model.Role;
import com.sangmo.fts.user.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

public class AuthorizeUserService {

    private static Logger LOG = LoggerFactory.getLogger(AuthorizeUserService.class);

    @Autowired private UserEntityService userEntityService;

    @Autowired private RoleEntityService roleEntityService;

    @Autowired private ClazzService clazzService;

    @Autowired private UserClazzEntityService userClazzEntityService;

    @Autowired private ExternalManagerService externalManagerService;

    public UserSession.Authorize<User> findAuthorize(String identity, String token) {
        User user = userEntityService.findByLoginName(identity);

        if (null != user) {
            if (!user.isEnabled()) {
                throw new ServiceException(StandardErrors.AUTHORIZED_WRONG.getStatus(), "error.account.disabled");
            }

            UserSession.Authorize<User> authorize = new UserSession.Authorize<>();
            authorize.setUser(user);

            Collection<String> perms = userEntityService.findPermissions(user.getId());

            authorize.setPermissions(perms);
            return authorize;
        } else {
            throw new ServiceException(StandardErrors.AUTHORIZED_WRONG.getStatus(), "error.account.wrong");
        }
    }

    public User doAuthentication(String identity, String password) {
        User user = userEntityService.findByLoginName(identity);
        if (null != user) {
            userEntityService.validatePassword(user.getId(), password);
            return user;
        } else {
            return null;
        }
    }


    public User doExternalAuthentication(String identity, String password) {
        LoginInfo loginInfo = externalManagerService.retrieveUsernfo(identity, password);
        if (null == loginInfo) {
            return null;
        }
        User user = userEntityService.findByLoginName(identity);
        if (null != user) {
            return userEntityService.update(user.getId(), user);
        } else {
            Role role = roleEntityService.createIfNotExists(loginInfo.getRoleId()+"");

            UserCreation userCreation = new UserCreation();
            userCreation.setLoginName(loginInfo.getUserName());
            userCreation.setName(loginInfo.getName());
            userCreation.setPassword(password);
            userCreation.setNumber(loginInfo.getUserName());
            userCreation.setRoleId(role.getId());
            userCreation.setExRoleId(loginInfo.getRoleId()+"");
            userCreation.setExUserId(loginInfo.getUserId()+"");
            userCreation.setSchoolName(loginInfo.getSchoolName());
            userCreation.setExSchoolId(loginInfo.getSchoolId()+"");
            User entity = userEntityService.create(userCreation);

            for(LoginInfo.Clazz clazzDto : loginInfo.getClassDtos()) {
                Clazz clazz = clazzService.createIfNotExists(clazzDto);
                userClazzEntityService.createIfNotExists(entity.getId(), clazz.getId());
            }
            return entity;
        }
    }






    public User createIfNotExists(String identity) {
        throw new ServiceException(StandardErrors.CLIENT_ERROR.getStatus(), "error.authorize.register.closed");
    }


}
